package com.example.sale.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    //阿里域名
    public static final String ALI_DOMAIN = "https://2023-08-02first-use-oss.oss-cn-chengdu.aliyuncs.com/";
    public static String uploadImage(MultipartFile file) throws IOException {
        String originalFileName = file.getOriginalFilename();
        String ext = "."+originalFileName.split("\\.")[1];
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid+ext;
        //生成新的文件名
        // 地域节点
        String endpoint ="http://oss-cn-chengdu.aliyuncs.com";
        String accessKeyID ="LTAI5tHWqPCpKEGQLDjHfbMn";
        String accessKeySecret ="DRom3iu9lq1IzKf7IqwX5AFXY0Acn4";
        //客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyID,accessKeySecret);
        ossClient.putObject(
                "2023-08-02first-use-oss",//仓库名称
                fileName,//文件名
                file.getInputStream()
        );
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }

}
